package com.skwen.delivery.superdelivery.myGreenDao.manager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.skwen.delivery.superdelivery.myGreenDao.DaoMaster;
import com.skwen.delivery.superdelivery.myGreenDao.DaoSession;
import com.skwen.delivery.superdelivery.myGreenDao.WriteOrderDao;
import com.skwen.delivery.superdelivery.order.bean.WriteOrder;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;


/**
 * Created by yundantian on 2017/10/12.
 */

public class WriteOrderDBManager {

    private final static String dbName = "write_order_db";
    private static WriteOrderDBManager mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;

    public WriteOrderDBManager(Context context) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static WriteOrderDBManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (WriteOrderDBManager.class) {
                if (mInstance == null) {
                    mInstance = new WriteOrderDBManager(context);
                }
            }
        }
        return mInstance;
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }


    /**
     * 插入一条记录
     *
     * @param writeOrder
     */
    public void insertWriteOrder(WriteOrder writeOrder) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        WriteOrderDao writeOrderDao = daoSession.getWriteOrderDao();
        writeOrderDao.insert(writeOrder);
    }

    /**
     * 插入订单集合
     *
     * @param writeOrders
     */
    public void insertWriteOrderList(List<WriteOrder> writeOrders) {
        if (writeOrders == null || writeOrders.isEmpty()) {
            return;
        }
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        WriteOrderDao WriteOrderDao = daoSession.getWriteOrderDao();
        WriteOrderDao.insertInTx(writeOrders);
    }


    /**
     * 删除一条记录
     *
     * @param writeOrder
     */
    public void deleteWriteOrder(WriteOrder writeOrder) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        WriteOrderDao WriteOrderDao = daoSession.getWriteOrderDao();
        WriteOrderDao.delete(writeOrder);
    }

    /**
     * 删除所有记录
     *
     */
    public void clearWriteOrder() {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        WriteOrderDao writeOrderDao = daoSession.getWriteOrderDao();
        writeOrderDao.deleteAll();
    }

    /**
     * 更新一条记录
     *
     * @param writeOrder
     */
    public void updateWriteOrder(WriteOrder writeOrder) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        WriteOrderDao WriteOrderDao = daoSession.getWriteOrderDao();
        WriteOrderDao.update(writeOrder);
    }

    /**
     * 查询订单列表
     */
    public List<WriteOrder> queryWriteOrderList() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        WriteOrderDao WriteOrderDao = daoSession.getWriteOrderDao();
        QueryBuilder<WriteOrder> qb = WriteOrderDao.queryBuilder();
        List<WriteOrder> list = qb.list();
        return list;
    }

}
